<?php
/**
 * 订单服务
 * Enter description here ...
 * @author Wolf
 *
 */
class OrderService {
	
	/**
	 * 打印未确认的订单
	 */
	public function p() {
		$verifyOrder = self::getOrderService ()->getVerifyOrder ();
		$this->setCsvHeader ();
		echo "序号,订单号,收货人,地区,商品名称,单价,数量,总额,备注";
		echo "\n";
		foreach ( $verifyOrder as $v ) {
			//备注中不能存在,号 和换行
			$remark = str_replace ( ",", "", $v ['remark'] );
			$remark = str_replace ( "<br>", "", $remark );
			echo sprintf ( "%s,%s,%s,%s,%s,%s,%s,%s,%s", $v ['id'], $v ['orderno'], $v ['shr'], $v ['address'], $v ['goods_name'], $v ['coupons'], $v ['num'], $v ['coupons_total'], $remark );
			echo "\n";
		}
	}
	/**
	 * 分页
	 *
	 * @return void
	 */
	protected function page($total, $num = 5) {
		$pageid = isset ( $_GET ['p'] ) ? $_GET ['p'] : 1;
		$start = ($pageid - 1) * $num;
		$pagenum = ceil ( $total / $num );
		$page = array ('start' => $start, 'num' => $num, 'current' => $pageid, 'page' => $pagenum );
		return $page;
	}
	private function createCSV($content) {
		header ( "Cache-Control: public" );
		header ( "Pragma: public" );
		header ( "Content-type:application/vnd.ms-excel" );
		$file = date ( "md", time () );
		header ( "Content-Disposition:attachment;filename=$file.csv" );
		echo $content;
	}
	/**
	 * 获取用户信息
	 * Enter description here ...
	 */
	private function getUserInfo() {
		$user = unserialize ( $_SESSION ['uid'] );
		return MemberModel::instance ()->getOneMember ( array ('uid' => $user ['uid'] ) );
	}
	
	/**
	 * 处理收入还是支出
	 * Enter description here ...
	 * @param unknown_type $order
	 */
	private function matchChargeTypes($order) {
		$chargeTypes = array_flip ( $this->chargeType );
		$couponsStatus = array_flip ( $this->status );
		foreach ( $order as $k => $v ) {
			$order [$k] ['chargetypes'] = $chargeTypes [$v ['chargetypes']];
			$order [$k] ['status'] = $couponsStatus [$v ['status']];
		}
		return $order;
	}
	private function setCsvHeader() {
		header ( "Cache-Control: public" );
		header ( "Pragma: public" );
		header ( "Content-type:application/vnd.ms-excel" );
		$file = date ( "md", time () );
		header ( "Content-Disposition:attachment;filename=$file.csv" );
	}
	private function input_csv($handle) {
		$out = array ();
		$n = 0;
		while ( $data = fgetcsv ( $handle, 10000 ) ) {
			$num = count ( $data );
			for($i = 0; $i < $num; $i ++) {
				$out [$n] [$i] = $data [$i];
			}
			$n ++;
		}
		return $out;
	}
	
	/**
	 * 获取产品信息
	 * @return 订单名称 总价  产品件数  产品列表
	 */
	private function getGoodsInfo($data, $sno) {
		$moneyTotal = 0;
		$couponsTotal = 0;
		$goodsArr = array ();
		$num = 0;
		$title = "";
		foreach ( $data as $k => $v ) {
			$rs = $this->getGoodsPrices ( $v ['id'] );
			if ($num == 0) {
				$title = $rs ['title'];
			}
			$goods = array ('orderno' => $sno, 'goods_id' => $v ['id'], 'num' => $v ['num'], 'goods_name' => $rs ['title'], 'prices' => $rs ['prices'], 'coupons' => $rs ['coupons'], 'money' => $rs ['prices'] * $v ['num'], 'coupons_total' => $rs ['coupons'] * $v ['num'] );
			$moneyTotal += $goods ['money'];
			$couponsTotal += $goods ['coupons_total'];
			$goodsArr [] = $goods;
			$num += $v ['num'];
		}
		return array ('name' => $title, 'moneytotal' => $moneyTotal, 'couponstotal' => $couponsTotal, 'goodslist' => $goodsArr, 'num' => $num );
	}
	/**
	 * 
	 * 扣除用户积分
	 * @param unknown_type $coupons
	 * @param unknown_type $uid
	 * @param unknown_type $remark
	 */
	private function saveUserCoupons($uid, $coupons, $remark, $payment, $chargetypes, $orderno, $status) {
		MemberModel::instance ()->saveCoupons ( $uid, - $coupons, $remark, $payment, $chargetypes, $orderno, $status );
	}
	/**
	 * 扣除用户金币
	 * Enter description here ...
	 * @param unknown_type $money
	 * @param unknown_type $uid
	 * @param unknown_type $remark
	 */
	private function saveUserMoney($uid, $money, $remark, $payment, $chargetypes, $orderno, $status) {
		CouponsModel::instance ()->saveMoney ( $uid, - $money, $remark, $payment, $chargetypes, $orderno, $status );
	}
	
	/**
	 * 
	 * 添加订单
	 * @param String $sno
	 * @param String $name 订单名称
	 * @param int $uid
	 * @param String $realname
	 * @param String $shr
	 * @param String $address
	 * @param String $mobile
	 * @param String $num
	 * @param decimal $coupons
	 * @param decimal $money
	 * @param String $remark
	 */
	private function addOrder($sno, $name, $uid, $realname, $shr, $address, $mobile, $num, $coupons, $money, $remark, $status) {
		$params = array ('uid' => $uid, 'name' => $name, 'orderno' => $sno, 'shr' => $shr, 'address' => $address, 'mobile' => $mobile, 'addtime' => time (), 'remark' => $remark, 'num' => $num, 'money' => $money, 'coupons' => $coupons, 'status' => $status );
		return OrderModel::instance ()->addOrder ( $params );
	
	}
	/**
	 * 增加订单明细  蒋getGoodsInfo中的goodsList 传递进来即可
	 * @param unknown_type $goodsList
	 */
	private function addGoods($goodsList) {
		foreach ( $goodsList as $v ) {
			OrderModel::instance ()->addGoods ( $v );
		}
		return true;
	}
	private function object_array($array) {
		if (is_object ( $array )) {
			$array = ( array ) $array;
		}
		if (is_array ( $array )) {
			foreach ( $array as $key => $value ) {
				$array [$key] = $this->object_array ( $value );
			}
		}
		return $array;
	}
	/**
	 * 获取产品扩展属性  产品价格和积分 代理价格 产品件数 销售
	 *
	 */
	private function getGoodsPrices($gid) {
		$key = "IntegralValue";
		//获取价格字段
		$rs = NewsModel::instance ()->getNewsWhere ( array ('id' => $gid ), 'id,title' );
		if (empty ( $rs ))
			return;
		$coupons = ExtendModel::instance ()->getExtendVarcharByWhere ( $gid, 1, $key );
		if (! empty ( $coupons )) {
			$rs ['coupons'] = $coupons [0] ['value'];
		} else {
			$rs ['coupons'] = 100000;
		}
		return $rs;
	}
}